﻿using Encloser.Packed;
using System.IO;

namespace Encloser.Json
{
    public class JsonItem
    {
        protected readonly JsonType type;
        private readonly string strVal;
        private readonly bool boolVal;
        private readonly double doubleVal;

        public JsonType Type { get => type; }
        internal virtual object Value { get { if (type == JsonType.Number) return doubleVal; if (type == JsonType.Boolean) return boolVal; return strVal; } }
        public JsonItem()
        {
            type = JsonType.Null;
        }

        protected JsonItem(JsonType type)
        {
            this.type = type;
        }

        public JsonItem(string value)
        {
            type = value == null ? JsonType.Null : JsonType.String;
            strVal = value;
        }

        public JsonItem(bool value)
        {
            type = JsonType.Boolean;
            boolVal = value;
        }

        public JsonItem(double value)
        {
            type = JsonType.Number;
            doubleVal = value;
        }

        public override string ToString()
        {
            var str = string.Empty;
            JsonParser.ToJsonString(this).Try(s => str = s);
            return str;
        }
    }
}
